package karaoke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Persona;
import karaoke.util.ConexionBD;

public class PersonaDAO extends BaseDAO {
	
	public Persona insertar(Persona p) throws DAOExcepcion{
		String query = "insert into persona(dni, nombres, paterno, materno) values(?, ?, ?, ?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, p.getDni());
			stmt.setString(2, p.getNombres());
			stmt.setString(3, p.getPaterno());
			stmt.setString(4, p.getMaterno());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return p;
	}
}
